package com.jykj.modbus.manage;

import com.jykj.modbus.equipment.entity.CollectInformation;
import com.jykj.modbus.utils.MathUtils;

import java.util.*;

/**
 * 德力西电气 DTS606型三相四线电子式电能表
 */
public class DlxElectricitySensor {

    /**
     * 解析返回的报文信息
     * @param hexStr 报文信息
     * @param type 返回结果类型(201:当前正向总电能;202:上1结算日正向有功总电能;)
     * @return
     */
    public static Map<String , Object> electricityMessageConversion(String hexStr , String type){
        Map<String , Object> mapData = new HashMap<>();
        List<CollectInformation> collectData = new ArrayList<CollectInformation>();
        if (type.equals("201")){
            //当前正向总电能
            CollectInformation collect1 = new CollectInformation();
            String s = hexStr.substring(34, 42);
            //将结果s转为分为4部分,每部分16进制的两个字节需要减33H,结果拼接为XXXXXX.XXKwh
            String i1 = mathData(s.substring(0 , 2) , "33");
            String i2 = mathData(s.substring(2 , 4) , "33");
            String i3 = mathData(s.substring(4 , 6) , "33");
            String i4 = mathData(s.substring(6 , 8) , "33");
            String data = i1+i4+i3+"."+i2;
            collect1.setTypeName("总电量");
            collect1.setValue(data);
            collect1.setUnit("Kwh");
            collect1.setType(type);
            collectData.add(collect1);
            mapData.put("CollectInformation" , collectData);

        }else if (type.equals("202")){
            //上1结算日正向有功总电能
            CollectInformation collect1 = new CollectInformation();
            String s = hexStr.substring(36, 44);
            //将结果s转为分为4部分,每部分16进制的两个字节需要减33H,结果拼接为XXXXXX.XXKwh
            String i1 = mathData(s.substring(0 , 2) , "33");
            String i2 = mathData(s.substring(2 , 4) , "33");
            String i3 = mathData(s.substring(4 , 6) , "33");
            String i4 = mathData(s.substring(6 , 8) , "33");
            String data = i1+i4+i3+"."+i2;
            collect1.setTypeName("上1结算日正向有功总电能");
            collect1.setValue(data);
            collect1.setUnit("Kwh");
            collect1.setType(type);
            collectData.add(collect1);
            mapData.put("CollectInformation" , collectData);
        }else if (type.equals("209")){
            //查询电表表号
            String s = hexStr.substring(10, 22);
            //将结果s两字符为一组,拼装为字符串集合
            String dataStr = new String();
            for (int e = 6 ; e>0 ; e--){
                String s1 = s.substring(e*2-2, e*2);
                dataStr += s1;
            }
            mapData.put("Instrument" , dataStr);
        }
        return mapData;
    }

    /**
     * 输入16进制减数与16进制被减数,输出16进制结果
     * @param hexStr
     * @param fortStr
     * @return
     */
    public static String mathData(String hexStr , String fortStr){
        int covert = MathUtils.covert(hexStr);
        int i = MathUtils.covert(fortStr);
        int i1 = covert - i;
        String s = MathUtils.intToHex(i1);
        if (s.length() == 1){
            s = "0"+s;
        }
        return s;
    }

    public static void main(String[] args) {
        String str = "FEFEFEFE6821450020622168910833333333BC4B3633AE16";
        String str1 = "FEFEFEFE682145002062216891083333";
        String str2 = "FEFEFEFE6821450020622168910833333333C64B3633B816";
        Map<String, Object> map = electricityMessageConversion(str, "201");
        System.out.println(map);
    }

}
